<template>
  <div>
    <el-tabs v-model="activeKey" @tab-click="handleTab">
      <el-tab-pane label="提现申请管理" name="1"> </el-tab-pane>
      <el-tab-pane label="提现批次管理" name="2"></el-tab-pane>
      <el-tab-pane label="提现手续费设置" name="3"></el-tab-pane>
    </el-tabs>
    <div v-if="activeKey=='1'">
    <el-card style="min-width: 1200px">
      <div class="tableSelectWrap">
        <el-form
          ref="withdrawRecordForm"
          :model="commandForm"
          label-width="100px"
        >
          <el-row>
            <el-col :span="7">
              <el-form-item label="选择时间" prop="date">
                <el-date-picker
                  :default-time="['00:00:00', '23:59:59']"
                  style="width: 100%"
                  v-model="commandForm.date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetimerange"
                  clearable
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1" >
              <el-form-item label="提现渠道" prop="payformChannel">
                <el-select
                  v-model="commandForm.payformChannel"
                  clearable
                  style="width: 100%"
                  placeholder="请选择提现渠道"
                >
                  <el-option
                    v-for="(item, index) of payChannel"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1">
              <el-form-item label="提现状态" prop="status">
                <el-select
                  v-model="commandForm.status"
                  clearable
                  style="width: 100%"
                  placeholder="请选择提现状态"
                >
                  <el-option
                    v-for="item of statusList"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col
              :span="7"
              style="min-width: 170px"
            >
              <el-button
                class="width120"
                @click="
                  resetForm('withdrawRecordForm');
                "
                >重置</el-button
              >
              <el-button
                type="primary"
                class="width120"
                @click="
                  () => {
                    this.dataForm = JSON.parse(
                      JSON.stringify(this.commandForm)
                    );
                    this.handleQuery();
                  }
                "
                >查询</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="textBox"
      style="min-width: 1200px; margin-bottom: -5px; margin-top: 15px"
    >
      <span class="numBox">
        <span>订单金额</span>
        <span class="symbol">￥</span>
        <span class="num">{{ info.fmtApplyDrawcashTotalAmount || 0.0 }}</span>
      </span>
      <span class="numBox">
        <span>已支付</span>
        <span class="symbol">￥</span>
        <span class="num">{{
          info.fmtFinishAuditDrawcashTotalAmount || 0.0
        }}</span>
      </span>
      <span class="numBox">
        <span>已赠送</span>
        <span class="symbol">￥</span>
        <span class="num">{{
          info.fmtFinishAuditDrawcashTotalAmount || 0.0
        }}</span>
      </span>
      <span class="numBox">
        <span>已提现</span>
        <span class="symbol">￥</span>
        <span class="num">{{
          info.fmtFinishAuditDrawcashTotalAmount || 0.0
        }}</span>
      </span>
    </el-card>
    <el-card style="margin-top: 15px; min-width: 1200px">
      <div class="switch">
        <el-switch v-model="isFixedTableH" inactive-text="固定表头">
        </el-switch>

        <div>
          <el-button v-if="isTep == 1" @click="handleClick" type="primary" class="width120">批量操作</el-button>
          <el-button v-if="isTep == 1"  type="primary" class="width120">导出</el-button>
          <div v-if="isTep == 2">
            <el-button @click="resetClick" class="width120">取消</el-button>
            <el-button
              type="primary"
              class="width120"
              @click="agreeAllFn(multipleSelection)"
              >批量同意</el-button
            >
            <el-button
              type="danger"
              class="width120"
              @click="refundAll(multipleSelection, false, true)"
              >批量拒绝</el-button
            >
          </div>
        </div>
      </div>
      <el-table
        :data="dataList"
        :height="fixedTableHeight"
        @selection-change="handleSelectionChange"
        ref="multipleTable"
        :key="activeKey"
      >
        <template slot="empty">
          <el-empty
            :image="require('@/assets/imgs/middleWork/k1.png')"
            :image-size="80"
            description="暂无相关数据"
          ></el-empty>
        </template>
        <el-table-column
          v-if="isPatch"
          type="selection"
          width="55"
          :selectable="(row) => row.status === 'wait_audit'"
        ></el-table-column>

        <el-table-column
          show-overflow-tooltip
          prop="gmtCreated"
          align="center"
          label="创建时间"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.gmtCreated ? scope.row.gmtCreated : "-" }}
            </div>
          </template>
        </el-table-column>

        <el-table-column
          show-overflow-tooltip
          prop="orderNo"
          align="center"
          label="用户ID"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.orderNo ? scope.row.orderNo : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="cashAmt"
          sortable
          align="center"
          label="提现金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtCashAmt ? scope.row.fmtCashAmt : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtTotalAmt"
          sortable
          align="center"
          label="渠道金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtTotalAmt ? scope.row.fmtTotalAmt : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="feeAmt"
          sortable
          align="center"
          label="用户手续费/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtFeeAmt ? scope.row.fmtFeeAmt : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="payformChannel"
          align="center"
          label="提现渠道"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{
                scope.row.payformChannel
                  ? CHANNEL_TYPE[scope.row.payformChannel]
                  : "-"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="status"
          align="center"
          label="提现状态"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.status? scope.row.status:'-'}}
            </div>
          </template>
        </el-table-column>

        <el-table-column
          show-overflow-tooltip
          prop="act"
          align="center"
          label="操作"
        >
          <template slot-scope="scope">
              <!-- <el-button
                type="text"
                @click="handleDetails(scope.row)"
                >详情</el-button
              > -->
              <span
                v-if="
                  scope.row.status == 'wait_audit' &&
                  getUserInfo().memberId == scope.row.cashMemberId
                "
              >
                <el-button
                  type="text"
                  @click="agreeFn()"
                  >同意</el-button
                >
                <el-button
                  type="text"
                  style="color: #f42b2b"
                  @click="handleSettle(scope.row, false, false)"
                  >拒绝</el-button
                >
              </span>
              <span v-else> - </span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      layout="prev, pager, next, sizes, jumper"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :current-page="pageNum"
      :total="total"
      @current-change="pageCurrentChangeHandle"
      @size-change="pageSizeChangeHandle"
    >
    </el-pagination>
  </div>
  <div v-if="activeKey=='2'">
    <el-card style="min-width: 1200px">
      <div class="tableSelectWrap">
        <el-form
          ref="withdrawRecordForm"
          :model="commandForm"
          label-width="100px"
        >
          <el-row>
            <el-col :span="7">
              <el-form-item label="选择时间" prop="date">
                <el-date-picker
                  :default-time="['00:00:00', '23:59:59']"
                  style="width: 100%"
                  v-model="commandForm.date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  type="datetimerange"
                  clearable
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col
              :span="7"
              style="min-width: 170px"
              :offset="1"
            >
            <el-button
                class="width120"
                @click="
                  resetForm('withdrawRecordForm');
                "
                >重置</el-button
              >
              <el-button
                type="primary"
                class="width120"
                @click="
                  () => {
                    this.dataForm = JSON.parse(
                      JSON.stringify(this.commandForm)
                    );
                    this.handleQuery();
                  }
                "
                >查询</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card style="margin-top: 15px; min-width: 1200px">
      <div class="switch">
        <el-switch v-model="isFixedTableH" inactive-text="固定表头">
        </el-switch>
      </div>

      <el-table
        :data="dataList"
        :height="fixedTableHeight"
      >
        <template slot="empty">
          <el-empty
            :image="require('@/assets/imgs/middleWork/k1.png')"
            :image-size="80"
            description="暂无相关数据"
          ></el-empty>
        </template>
        <el-table-column
          show-overflow-tooltip
          prop="gmtCreated"
          align="center"
          label="创建时间"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.gmtCreated ? scope.row.gmtCreated : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="batchId"
          align="center"
          label="批次号"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.batchId ? scope.row.batchId : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtTotalAmt"
          sortable
          align="center"
          label="提现金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtTotalAmt ? scope.row.fmtTotalAmt : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtTotalFeeAmt"
          sortable
          align="center"
          label="用户手续费/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtTotalFeeAmt ? scope.row.fmtTotalFeeAmt : "0.00" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="result"
          align="center"
          label="结果"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.result ? scope.row.result : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="act"
          align="center"
          label="操作"
        >
          <!-- <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleDetails(scope.row)"
                >详情</el-button>
              </template> -->
              <div>-</div>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      layout="prev, pager, next, sizes, jumper"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :current-page="pageNum"
      :total="total"
      @current-change="pageCurrentChangeHandle"
      @size-change="pageSizeChangeHandle"
    >
    </el-pagination>
  </div>
  <div v-if="activeKey=='3'">
    <el-card style="min-width: 1200px">
        <el-form 
          ref="withdrawRecordForm"
          :model="commandForm"
          label-width="135px"
        >
          <el-row>
            <el-col :span="14">
              <el-form-item label="用户提现手续费">
                <div v-if="editShow">
                  <el-select
                  class="leftSelect"
                  v-model="commandForm.dateType"
                style="width:50%"
                  placeholder="收取用户手续费"
                >
                  <el-option label="收取用户手续费" value="1"></el-option>
                  <el-option label="不收取" value="2"></el-option>
                </el-select>
                <el-input style="width:40%;margin-left: 20px;" v-model="commandForm.fee"   placeholder="请输入" v-if="commandForm.dateType=='1'">
                  <template slot="append">%</template>
                </el-input>
                </div>
  <div v-else style="color:#03BFC6;margin-left:10px ;">不收取</div>
              </el-form-item>
            </el-col>
            <el-col :span="4" :offset="6" >
              <div v-if="editShow">
                <el-button
                class="width120"
                @click="editShow=false"
                >取消</el-button>
                <el-button
                type="primary"
                class="width120"
                @click="editShow=false">保存</el-button>
              </div>
              <el-button v-else
                type="primary"
                class="width120"
                @click="editShow=true"
                style="margin-left:125px;"
                >修改</el-button>
            </el-col>
          </el-row>
        </el-form>
    </el-card>


  </div>
    <!-- <details-drawer :drawer.sync="drawer" :info="detailsInfo"></details-drawer> -->
  </div>
</template>

<script>
import { setTabIndex, getTabIndex, getUserInfo } from "@/utils/public";
import mixin from "@/mixins/mixin";
// import detailsDrawer from "@/components/detailsDrawer.vue";
export default {
  name: "withdrawRecord",
  mixins: [mixin],
  components: {
    // detailsDrawer,
  },
  data() {
    return {
      editShow:false,
      // buttonRole:false,
      multipleSelection: [],
      isPatch: false, //批量切换
      isTep: 1, //表格按钮切换
      activeKey: getTabIndex(), //切换tab
      mixinViewModuleOptions: {
        getDataListURL: "/api/merchant/settleBalanceDrawcash/searchByPage",
        createdIsNeed: false,
      },
      drawer: false,
      commandForm: {
        payformChannel: "",
        status: "",
        date: [],
      },
      detailsInfo: {},
      payChannel: this.$store.state.channelList,
      statusList: [
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "待审核", value: "wait_audit" },
        { id: 3, label: "审核拒绝", value: "refused_audit" },
        { id: 4, label: "提现处理中", value: "pending_drawcash" },
        { id: 5, label: "提现成功", value: "success_drawcash" },
        { id: 6, label: "提现失败", value: "fail_drawcash" },
      ],
      info: {},
    };
  },
  created() {
    // if (this.$store.state.buttonList) {
    //   this.$store.state.buttonList.forEach((e) => {
    //     if (e.type == "withdrawalApply") {
    //       this.buttonRole = true;
    //     }
    //   });
    // }
  },
  mounted() {
    this.dataForm = JSON.parse(JSON.stringify(this.commandForm));
    this.handleTab();
  },
  watch: {
    "$store.state.testLine": {
      handler() {
        if (this.$route.name == this.$options.name) {
          this.handleTab();
        }
      },
    },
    deep: true,
  },
  methods: {
    getUserInfo,
    getTabIndex,
    handleSettle(row, status, isBatch) {
      this.$confirm("确定执行该操作?", "提示", {
        customClass: "confirm",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        showClose: false,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        type: "warning",
        center: true,
      }).then(() => {
        let auditList = [
          {
            id: row.id,
            payformChannel: row.payformChannel,
          },
        ];
        this.$http
          .post(`/api/merchant/settleBalanceDrawcash/batchAudit`, {
            auditStatus: status,
            isBatch: isBatch,
            auditList,
          })
          .then((res) => {
            if (res.code === "0000") {
              this.$message.success("审核已完成！");
              this.handleQuery();
            }
          });
      });
    },
    agreeAllFn(row){
if (row.length === 0) {
        return this.$message.warning("请先选择提现申请订单");
      } else {
this.$router.push('/account/withdrawRecord/withdrawAudit')
      }
    },
    agreeFn(){
this.$router.push('/account/withdrawRecord/withdrawAudit')
    },
    refundAll(row, status, isBatch) {
      if (row.length === 0) {
        return this.$message.warning("请先选择提现申请订单");
      } else {
        this.$confirm("确定执行该操作?", "提示", {
          customClass: "confirm",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showClose: false,
          closeOnClickModal: false,
          closeOnPressEscape: false,
          type: "warning",
          center: true,
        }).then(() => {
          let auditList = [];
          row.forEach((e) => {
            auditList.push({ id: e.id, payformChannel: e.payformChannel });
          });
          this.$http
            .post(`/api/merchant/settleBalanceDrawcash/batchAudit`, {
              auditStatus: status,
              isBatch: isBatch,
              auditList,
            })
            .then((res) => {
              if (res.code === "0000") {
                this.$message.success("审核已完成！");
                this.resetClick();
                this.handleQuery();
              }
            });
        });
      }
    },
    handleTab() {
      setTabIndex(this.activeKey);
      this.isPatch = false;
      this.isTep = 1;
      this.multipleSelection = [];
      this.resetForm("withdrawRecordForm");
      this.pageSize = 10; //重置每页条数
      this.handleQuery();
    },
    handleClick() {
      this.isPatch = true;
      this.isTep = 2;
    },
    resetClick() {
      this.isPatch = false;
      this.isTep = 1;
      this.multipleSelection = [];
      this.$refs.multipleTable.clearSelection();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDetails(row) {
      this.$router.push({
        path: "/account/withdrawRecord/detail",
        query: { id: row.batchId, memberId: row.memberId },
      });
    },
    handleQuery() {
      if (this.dataForm.date && this.dataForm.date.length) {
        this.dataForm.gmtCreatedBegin = this.dataForm.date[0];
        this.dataForm.gmtCreatedEnd = this.dataForm.date[1];
      } else {
        this.dataForm.gmtCreatedBegin = null;
        this.dataForm.gmtCreatedEnd = null;
      }
      if (this.activeKey == "1") {
        this.dataList=[]
       this.mixinViewModuleOptions.getDataListURL="/api/merchant/settleBalanceDrawcash/searchByPage"
       this.getStatisticsInfo();
       this.getDataList();
      } else if(this.activeKey == "2"){
        this.dataList=[]
        this.mixinViewModuleOptions.getDataListURL="/api/merchant/settleBalanceDrawcash/searchBatchAuditByPage"
      this.getDataList();
      }
    },
    getStatisticsInfo() {
      this.$http
        .post(
          "/api/merchant/settleBalanceDrawcash/balanceDrawcashStatistics",
          this.dataForm
        )
        .then((res) => {
          if (res.code === "0000") {
            this.info = res.data || {};
          }
        });
    },
  },
};
</script>

<style scoped lang="scss">
.pedding {
  color: #f69c1f;
}
.succeed {
  color: #07a555;
}
.failed {
  color: #f42b2b;
}
.switch {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}
.ml {
  margin-left: 50px;
}
.search {
  position: absolute;
  right: 0;
  top: -164px;
}
</style>
